#include "butterfly.h"
#include "ui_butterfly.h"
#include<qrect.h>
#include <QPixmap>
//#include <ctime>
#include <cstdlib>
#include <QKeyEvent>
//#include <qstring.h>

Butterfly::Butterfly(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Butterfly),blsRun(false),speed(2000)
{
    ui->setupUi(this);
    this->grabKeyboard();
    //this->setGeometry(QRect(300,300,500,500));//在构造函数里写明窗体显示在什么位置，如规定窗体显示在以坐标（300,300）为起始点，大小为 500*500 的矩形（QRect）范围内
}

Butterfly::~Butterfly()
{
    delete ui;
}


void Butterfly::InitButterfly()//初始化Piyonom
{
    nDirection=2;//默认向下运动
    blsRun=true;
    blsOver=false;

    x = 100;
    y = 250;
    nscore = 0;
    timer= new QTimer(this);
    timer->start(500);
    connect(timer,SIGNAL(timeout()),SLOT(Butterfly_update()));
    //三根柱子的初始坐标
    xx1=300;
    xx2=600;
    xx3=900;
    yy1 = rand()%271;
    d1 = rand()%100 + 120;
    yy2 = rand()%271;
    d2 = rand()%100 + 120;
    yy2 = rand()%271;
    d2 = rand()%100 + 120;

}

void Butterfly::paintEvent(QPaintEvent *event)
{

    if(!blsRun)
    {
        //第一次运行时需要初始化Piyomon
        InitButterfly();
    }
    QPainter painter(this);
    //背景
    painter.drawPixmap(0, 0, 900, 500,QPixmap(":/myImage/images/back.jpg"));//插入背景图片
    //Piyomon

        painter.drawPixmap(x,y,60,70,QPixmap(":/myImage/images/Piyomon.png"));//插入Piyomon图片
        //插入Piyomon的图片, 起始坐标(x, y), 大小60*70
        if(y==500||y==0)//判断Piyomon是不是掉下去了
        blsOver=true;//游戏结束

    //柱子
    painter.setPen(Qt::white);//设置画笔颜色
    painter.setBrush(Qt::white);//设置填充颜色
    //第一根柱子
    painter.drawRect(xx1, 0, 60, yy1);//上面的柱子
    painter.drawRect(xx1, yy1+d1, 60, 490-d1-yy1);//下面的柱子
    //第二根柱子
    painter.drawRect(xx2, 0, 60, yy2);//上面的柱子
    painter.drawRect(xx2, yy2+d2, 60, 490-d2-yy2);//下面的柱子
    //第三根
    painter.drawRect(xx3, 0, 60, yy2);//上面的柱子
    painter.drawRect(xx3, yy2+d2, 60, 490-d2-yy2);//下面的柱子

    ui->label_2->setText(QString::asprintf("%d", nscore));//写入得分
    ui->start->setText("START");

    if(blsOver)//游戏结束，停止计时器
    {
        timer->stop();
        ui->start->setText("RESTART");
        painter.drawPixmap(0,0,900,500,QPixmap(":/myImage/images/lost.jpg"));//页面变换为“you lost”
    }
    if(nscore>=500)
    {
        timer->stop();
        ui->start->setText("RESTART");
        painter.drawPixmap(0,0,900,500,QPixmap(":/myImage/images/win.jpg"));//页面变换为“you win”
    }
}

void Butterfly::keyPressEvent(QKeyEvent *key)//键盘事件
{
    switch(key->key())
    {
        case Qt::Key_Space:
            nDirection = 1;
            break;
        default:;
    }
}

void Butterfly::keyReleaseEvent(QKeyEvent *key)//松键盘时Piyomon继续掉落
{
    switch(key->key())
    {
        case Qt::Key_Space://按空格键,Piyomon上升
            nDirection = 2;
            break;
        default:;
    }
}

void Butterfly::Butterfly_update()//更新界面
{
    if(nDirection==1)
    {
        y = y - 40;//Piyomon上升40
        nDirection = 0;
    }
    else
        y = y + 20;//Piyomon下降20

    //柱子向左移动
    xx1 = xx1 - 20;
    xx2 = xx2 - 20;
    xx3 = xx3 - 20;
    //判断柱子是否到达界面左端,如果是,回到界面右端
    if(xx1==-60)
    {
        xx1 = 900;
        yy1 = rand()%271;
        d1 = rand()%100 + 120;
    }
    if(xx2==-60)
    {
        xx2 = 900;
        yy2 = rand()%271;
        d2 = rand()%100 + 120;
    }

    if(xx3==-60)
    {
        xx3 = 900;
        yy2 = rand()%271;
        d2 = rand()%100 + 120;
    }

    //判断Piyomon是否撞到柱子
    if((x>=xx1-60)&&(x<=xx1+60))
    {
        if((y>=yy1)&&(y<=yy1+d1-70))
        {
            nscore = nscore + 10;//加分
        }
        //else
        //{
            //blsOver=true;//游戏结束
        //}
    }
    if((x>=xx2-60)&&(x<=xx2+60))
    {
        if((y>=yy2)&&(y<=yy2+d2-70))
        {
            nscore = nscore + 10;
        }
        //else
        //{
            //blsOver=true;
        //}
    }
    if((x>=xx3-60)&&(x<=xx3+60))
    {
        if((y>=yy3)&&(y<=yy3+d3-70))
        {
            nscore = nscore + 10;
        }
        //else
        //{
            //blsOver=true;
        //}
    }
    update();//paintEvent更新
}

void Butterfly::on_start_clicked()
{
    InitButterfly();//鼠标点击"start"时,初始化蝴蝶,游戏重新开始
}


void Butterfly::on_pushButton_clicked()
{
    blsOver=true;
}